#version 330

uniform sampler2D InSampler;
in vec2 texCoord;
out vec4 fragColor;

float getChar(int charId, vec2 f) {
    if (f.x < 0.1 || f.x > 0.9 || f.y < 0.1 || f.y > 0.9) return 0.0;
    if (charId == 1) return (abs(f.x - 0.5) < 0.1 || abs(f.y - 0.5) < 0.1) ? 1.0 : 0.0;
    if (charId == 2) return (f.x > f.y) ? 1.0 : 0.0;
    if (charId == 3) return (f.x > 0.3 && f.x < 0.7 && f.y > 0.3 && f.y < 0.7) ? 0.0 : 1.0;
    if (charId == 4) return 1.0;
    return 0.0;
}

void main() {
    vec2 gridSize = vec2(8.0, 12.0);
    vec2 pixelPos = gl_FragCoord.xy;
    vec2 cellCoord = floor(pixelPos / gridSize) * gridSize;
    
    // Fixed: Changed fraction to fract
    vec2 localUV = fract(pixelPos / gridSize);
    
    vec2 targetUV = cellCoord / vec2(textureSize(InSampler, 0));
    vec4 rawColor = texture(InSampler, targetUV);
    float luma = dot(rawColor.rgb, vec3(0.299, 0.587, 0.114));
    
    int charId = 0;
    if (luma > 0.2) charId = 1;
    if (luma > 0.4) charId = 2;
    if (luma > 0.6) charId = 3;
    if (luma > 0.8) charId = 4;
    
    float visibility = getChar(charId, localUV);
    vec3 greenPhosphor = vec3(0.0, 1.0, 0.2) * visibility * (luma * 0.5 + 0.5);
    
    fragColor = vec4(greenPhosphor, rawColor.a);
}